package com.cdtu.store.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdtu.store.common.utils.PageUtils;
import com.cdtu.store.common.utils.R;
import com.cdtu.store.common.entity.CategoryBrandEntity;
import com.cdtu.store.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;



/**
 * 商品分类和品牌的中间表，两者是多对多关系
 *
 * @author dongyuhao
 * @date 2020-06-22 23:46:27
 */
@RestController
@RequestMapping("categorybrand")
public class CategoryBrandController {
    @Autowired
    private CategoryBrandService categoryBrandService;

    /**
     * 查询当前分类下的所有品牌
     */

    @GetMapping("/catelog/brands/{cid}")
    public R getBrandsByCid(@PathVariable("cid") Long cid){
        List<CategoryBrandEntity> data = categoryBrandService.list(
                new QueryWrapper<CategoryBrandEntity>().eq("category_id",cid));
        return R.ok().put("data",data);
    }



    /**
     * 获取当前品牌关联的所有分类列表
     * @param brandId
     * @return
     */
    @GetMapping("/catelog/list")
    public R categorylist(@RequestParam("brandId") Long brandId){
        List<CategoryBrandEntity> data = categoryBrandService.list(
                new QueryWrapper<CategoryBrandEntity>().eq("brand_id",brandId));
        return R.ok().put("data", data);
    }
    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{categoryId}")
    public R info(@PathVariable("categoryId") Long categoryId){
		CategoryBrandEntity categoryBrand = categoryBrandService.getById(categoryId);

        return R.ok().put("categoryBrand", categoryBrand);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryBrandEntity categoryBrand){

		categoryBrandService.saveDetail(categoryBrand);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody CategoryBrandEntity categoryBrand){
		categoryBrandService.updateById(categoryBrand);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody CategoryBrandEntity categoryBrand){
		categoryBrandService.removeCategoryBrand(categoryBrand);
        return R.ok();
    }

}
